package cmpe451.group7.ourcity.beans;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import cmpe451.group7.ourcity.entity.Tag;

/**
 * This is for converting the Tag entity to a object that can be displayed in
 * the autocomplete textboxes
 */
@FacesConverter(value = "tagConverter")
public class TagConverter implements Converter {

	/**
	 * Convert string representation of the Tag to Object
	 */
	@Override
	public Object getAsObject(FacesContext context, UIComponent component,
			String value) {
		String[] values = value.split(",");

		Tag tag = new Tag();
		tag.setmID(values[0]);
		tag.setTagName(values[1]);
		if (values.length == 3)
			tag.setNotableName(values[2]);
		else
			tag.setNotableName("");

		return tag;
	}

	/**
	 * Convert the object to string representation
	 */
	@Override
	public String getAsString(FacesContext context, UIComponent component,
			Object value) {
		Tag tag = (Tag) value;
		return String.format("%s,%s,%s",
				tag.getmID() == null ? "" : tag.getmID(), tag.getTagName(),
				tag.getNotableName() == null ? "" : tag.getNotableName());
	}

}
